<?php

/**
 * 	本果用户模块
 *	用户注册时, 要根据 inviteCode 来确定上级 userID.
 *  注册 和 修改用户信息时, 都要同时修改 bg_users_relationships 里面的 user_nicename.
 */

namespace McUser\Controller;
use Common\Controller\HomebaseController;

class McUserController extends HomebaseController {

	function _initialize(){}
	
	/*
	*	得到IP地址.
	*/
	function getip()
	{
	    if(!empty($_SERVER["HTTP_CLIENT_IP"]))
	    {
	        $cip = $_SERVER["HTTP_CLIENT_IP"];
	    }
	    else if(!empty($_SERVER["HTTP_X_FORWARDED_FOR"]))
	    {
	        $cip = $_SERVER["HTTP_X_FORWARDED_FOR"];
	    }
	    else if(!empty($_SERVER["REMOTE_ADDR"]))
	    {
	        $cip = $_SERVER["REMOTE_ADDR"];
	    }
	    else
	    {
	        $cip = '';
	    }

	    preg_match("/[\d\.]{7,15}/", $cip, $cips);
	    $cip = isset($cips[0]) ? $cips[0] : 'unknown';
	    unset($cips);
	    return $cip;
	}

	/*
	*	注册函数.
	*/ 
	public function register(){
		$mobile = $_POST['mobile'];
		$pass = $_POST['pass'];
		$verify_code = $_POST['verifyCode'];
		$inviteCode = $_POST['inviteCode'];
		$inviteCode = trim($inviteCode);

		$code = C('retJson');
		/* 传入参数不能为空 */ 
		if ($mobile == '' || $pass == '' || $verify_code == ''){
			$rejson['retCode'] = "-2";
			$rejson['retMsg'] = $code['-2'];
			$this -> ajaxReturn ( $rejson );
			return;
		}

		$rejson = array(
			"retCode" => "1",
			"retMsg" => $code['1'] );

		/* 检查手机号码是否已经注册 */
		$verify_his = D('VerifyHis');

		/*1, 检查手机号码是否已经注册 */
		$reg = $verify_his -> check_mobile_registered($mobile);
		if ($reg){
			/* 1, 手机号码注册, 手机号码已经注册 */
			$rejson['retCode'] = '-104';
			$rejson['retMsg'] = $code['-104'];

			$this -> ajaxReturn($rejson);
			return;
		}

		/* 检查验证码是否正确 */		
		$check_verify = $verify_his -> check_verify_code($mobile,$verify_code);
		if ($check_verify == "-102" ){
			/* 短信验证码错误 */
			$rejson['retCode'] = "-102";
			$rejson['retMsg'] = $code['-102'];
		}
		elseif ($reVerify == "-103" ){
			/* 短信验证码失效 */
			$rejson['retCode'] = "-103";
			$rejson['retMsg'] = $code['-103'];	
		}
		else{
			$relation = D('McUsersRelationships');
			$pUserID = '0';
			$pNiceName = '';

			/*  邀请码 - 检查邀请是否正确, 如果不正确, 则返回, 如果正确, 在返回上级昵称. */
			/*  				邀请码不正确, 不能注册.  								 */
			if($inviteCode)
			{
				$pUserID = $relation -> check_invite_code( $inviteCode );
				if ( ! $pUserID )
				{
					$rejson['retCode'] = "-113";
					$rejson['retMsg'] = $code['-113'];
					$this->ajaxReturn($rejson); 
					return;				
				}

				$user = D('McUser');
				$d = $user -> read_user_info($pUserID);
				$pNiceName = $d['user_nicename'];
			}

			/* 验证码正确, 保存注册信息 */
			$user = D('McUser');
			$ip = $this -> getip();
			$user -> save_register_info($mobile, $pass, $ip);

			/*    注册后, 要注意保存 用户关系, 返回 用户ID    */
			$info = $user -> read_user_info_by_mobile($mobile);
			$id = $info['id'];
			$user_nicename = $info['user_nicename'];
			
			// 上下级关系保存
			if($pNiceName){
				$relation -> add_relationship($id, $user_nicename, $pUserID, $pNiceName);
			}

			$data['userID'] = $id;
			$data['user_nicename'] = $user_nicename;
			$data['pNiceName'] = $pNiceName;

			$rejson['data'] = $data;
		}

		$this->ajaxReturn($rejson); 
	}
	
	/*
	*	登陆函数
	*/
	public function login(){
		$mobile = $_POST['mobile'];
		$pass = $_POST['pass'];
		
		$code = C('retJson');
		$rejson = array(
			"retCode" => "1",
			"retMsg" => $code['1'] );
						
		/* 传入参数不能为空 */ 
		if ($mobile == '' || $pass == ''){
			/* 传入参数不正确 */
			$rejson['retCode'] = "-2";
			$rejson['retMsg'] = $code['-2'];
			$this -> ajaxReturn ( $rejson );
			return;
		}

		/* 在模型里面进行判断, 用户名和密码是否正确 */
		$user = D('McUser');
		$login_data = $user -> mobile_login($mobile, $pass);
		
		/*
		dump($login_data);
		*/

		if ($login_data['retCode'] == '-105'){
			/* 手机号码或是密码错误 */
			$rejson['retCode'] = "-105";
			/* $rejson['retMsg'] = "Mobile Phone Number or Password Error";  */
			$rejson['retMsg'] = $code['-105'];
			
		}
		else{
			/* 登陆成功, 生成一个token */
			$token_model = D('Token');
			$token = $token_model -> create_token($mobile);

			/* 把这个 token 保存 */ 
			$ip = $this -> getip();
			$token_model -> save_token($mobile, $ip, $token);

			/* 组织返回的数据 */ 
			$data['mobile'] = $mobile;
			$data['token'] = $token;

			$data['userID'] = $login_data['id'];
			
			$data['user_nicename'] = $login_data['user_nicename'];
			$data['user_email'] = $login_data['user_email'];
			$data['sex'] = $login_data['sex'];
			$data['birthday'] = $login_data['birthday'];
			$data['signature'] = $login_data['signature'];
			$data['last_login_ip'] = $login_data['last_login_ip'];
			$data['last_login_time'] = $login_data['last_login_time'];
			$data['create_time'] = $login_data['create_time'];

			/* 读取邀请码 */
			$relation = D('McUsersRelationships');
			$pUser = $relation -> read_info($login_data['id']);
			$data['inviteCode'] = $pUser['inviteCode'];
			$data['pUserID'] = $pUser['pUserID'];
			$data['pNiceName'] = $pUser['pNiceName'];

			$rejson['data'] = $data;
		}

		$this -> ajaxReturn ($rejson) ;		
	}

	/*
	*	读取用户信息
	*/
	public function readUserInfo(){
		$userID = $_POST['userID'];
		$token = $_POST['token'];

		$code = C('retJson');
		$rejson = array(
			"retCode" => "1",
			"retMsg" => $code['1'] );
						
		/* 传入参数不能为空 */ 
		if ($userID == '' || $token == ''){
			/* 传入参数不正确 */
			$rejson['retCode'] = "-2";
			$rejson['retMsg'] = $code['-2'];
			$this -> ajaxReturn ( $rejson );
			return;
		}

		/* 检查 token 是否正确 */ 
		$token_model = D('Token');
		$compare = $token_model -> compare_token_by_userID($userID, $token);
		if (! $compare){
			/* 手机号或者 Token 错误 */
			$rejson['retCode'] = "-111";
			$rejson['retMsg'] = $code['-111'];
			$this -> ajaxReturn ($rejson) ;		
		}

		/* 读取数据 */
		$user = D('McUser');
		$login_data = $user -> read_user_info($userID);
			$data['userID'] = $userID;
			$data['user_nicename'] = $login_data['user_nicename'];
			$data['user_email'] = $login_data['user_email'];
			$data['sex'] = $login_data['sex'];
			$data['birthday'] = $login_data['birthday'];
			$data['signature'] = $login_data['signature'];
			$data['last_login_ip'] = $login_data['last_login_ip'];
			$data['last_login_time'] = $login_data['last_login_time'];
			$data['create_time'] = $login_data['create_time'];

			$rejson['data'] = $data;

		$this -> ajaxReturn ($rejson) ;		
	}

	/**
	 * 读取公开的用户信息.
	 */
	public function readUserInfoOfPublic(){
		$userID = $_POST['userID'];

		if ( $userID == '' ){
			$this->return_data_error('-2') ;
			return;				
		}
		
		/* 读取数据 */
		$user = D('McUser');
		$login_data = $user -> read_user_info($userID);
		
			$data['userID'] = $userID;
			$data['user_nicename'] = $login_data['user_nicename'];
			$data['sex'] = $login_data['sex'];
			$data['birthday'] = $login_data['birthday'];
			$data['signature'] = $login_data['signature'];

		$this -> return_data ($data) ;		
	}


	/*
	*	注销, 要删除token
	*/
	public function logout(){
		$mobile = $_POST['mobile'];
		$token = $_POST['token'];

		/* 检查 token 是否正确 */ 
		$token_model = D('Token');
		$compare = $token_model -> compare_token_by_mobile($mobile, $token);

		$code = C('retJson');

		$rejson = array(
			"retCode" => "1",
			"retMsg" => $code['1'] );

		if ($compare){
			/* token 比对正确后, 删除此 token 信息 */
			$token_model -> delete_token($mobile);			
		}
		else{
			/* 手机号或者 Token 错误 */
			$rejson['retCode'] = "-111";
			$rejson['retMsg'] = $code['-111'];
		}

		$this -> ajaxReturn ( $rejson );
	}

	/*
	*	修改用户密码
	*/
	public function changePass(){
		$userID = $_POST['userID'];
		$token = $_POST['token'];
		$passOld = $_POST['passOld'];
		$passNew = $_POST['passNew'];

		if ( $token == '' || $userID == '' || $passOld == '' || $passNew == '' ){
			$this->return_data_error('-2') ;
			return;				
		}

		/* 检查 token 是否正确 */ 
		$token_model = D('Token');
		$compare = $token_model -> compare_token_by_userID($userID, $token);

		$user = D('McUser');
		$result = $user -> userID_login($userID, $passOld);		
		$comparePass = '1'		;
		if ($result['retCode'] != '1')
			$comparePass = '';

		$code = C('retJson');

		$rejson = array(
			"retCode" => "1",
			"retMsg" => $code['1'] );
		
		if ($compare && $comparePass){
			/* token 比对正确后, 删除此 token 信息 */
			/* $token_model -> delete_token($userID);			 */			
			$ip = $this -> getip();
			$user -> reset_pass_by_userID($userID, $passNew, $ip);
		}
		else{
			/* 手机号或者 Token 错误 */
			$rejson['retCode'] = "-112";
			$rejson['retMsg'] = $code['-112'];
		}
		$this -> ajaxReturn ( $rejson );
	}

	/*
	*	重设密码
	*/
	public function resetPass(){
		$mobile = $_POST['mobile'];
		$pass = $_POST['pass'];
		$verify_code = $_POST['verifyCode'];

		$code = C('retJson');

		$rejson = array(
			"retCode" => "1",
			"retMsg" => $code['1'] );

		/*
		echo $mobile;
		echo $pass;
		echo $verify_code;
		*/
		
		/* 传递进来的数据不能为空. */ 
		if ($mobile == '' || $pass == '' || $verify_code =='' ){
			$rejson['retCode'] = "-2";
			//$rejson['retMsg'] = "Illegal Request Parameter";
			$rejson['retMsg'] = $code['-2'];

			$this -> ajaxReturn ($rejson) ;
			return;
		}

		/* 检查验证码是否正确 */
		$verify_his = D('VerifyHis');
		$check_verify = $verify_his -> check_verify_code($mobile,$verify_code);
		if ($check_verify == "-102" ){
			/* 短信验证码错误 */
			$rejson['retCode'] = "-102";
			$rejson['retMsg'] = $code['-102'];
		}
		elseif ($reVerify == "-103" ){
			/* 短信验证码失效 */
			$rejson['retCode'] = "-103";
			$rejson['retMsg'] = $code['-103'];
		}
		else{
			/* 验证码正确, 修改密码 */						
			$user = D('McUser');
			$ip = $this -> getip();
			$user -> reset_pass_by_mobile($mobile, $pass, $ip);
		}

		$this -> ajaxReturn($rejson);					
	}

	/*
	*	修改手机
	*/
	public function changeMobile(){
		$mobileOld = $_POST['mobileOld'];
		$mobileNew = $_POST['mobileNew'];
		$verify_code_old = $_POST['verifyCodeOld'];
		$verify_code_new = $_POST['verifyCodeNew'];

		$code = C('retJson');

		$rejson = array(
			"retCode" => "1",
			"retMsg" => $code['1'] );

		/*
		echo $mobile;
		echo $pass;
		echo $verify_code;
		*/
		
		/* 传递进来的数据不能为空. */ 
		if ($mobileOld == '' || $mobileNew == '' || $verify_code_old =='' || $verify_code_new =='' ){
			$rejson['retCode'] = "-2";
			$rejson['retMsg'] = $code['-2'];
			$this -> ajaxReturn ($rejson) ;
			return;
		}

		/* 检查验证码是否正确 */
		$verify_his = D('VerifyHis');
		$check_verify_old = $verify_his -> check_verify_code($mobileOld,$verify_code_old,'2');
		$check_verify_new = $verify_his -> check_verify_code($mobileNew,$verify_code_new,'1');
		if ($check_verify_old == "-102" || $check_verify_new == "-102" ){
			/* 短信验证码错误 */
			$rejson['retCode'] = "-102";
			$rejson['retMsg'] = $code['-102'];	
		}
		elseif ($check_verify_old == "-103" || $check_verify_new == "-103" ){
			/* 短信验证码失效 */
			$rejson['retCode'] = "-103";
			$rejson['retMsg'] = $code['-103'];
		}
		else{
			/* 验证码正确, 修改密码 */						
			$user = D('McUser');
			$ip = $this -> getip();
			$user -> changeMobile($mobileOld, $mobileNew, $ip);
		}

		$this -> ajaxReturn($rejson);					
	}

	/*  
	*	修改用户信息: 昵称 / 性别:0保密1男2女 / 邮箱 / 签名
	*/
	public function changeUserInfo(){
		$userID = $_POST['userID'];
		$user_nicename = $_POST['user_nicename'];
		$sex = $_POST['sex'];
		$email = $_POST['email'];
		$signature = $_POST['signature'];

		$token = $_POST['token'];

		$code = C('retJson');

		$rejson = array(
			"retCode" => "1",
			"retMsg" => $code['1'] );

		/* 传递进来的数据不能为空. */ 
		if ($userID == ''){
			$rejson['retCode'] = "-2";
			$rejson['retMsg'] = $code['-2'];
			$this -> ajaxReturn ($rejson) ;
			return;
		}

		/* 检查 token 是否正确 */ 
		$token_model = D('Token');
		$compare = $token_model -> compare_token_by_userID($userID, $token);
		if (! $compare){
			/* 手机号或者 Token 错误 */
			$rejson['retCode'] = "-111";
			$rejson['retMsg'] = $code['-111'];
			$this -> ajaxReturn ($rejson) ;		
		}

		$sex = trim($sex);

		if ($sex == '男')
			$sex = '1';

		if ( $sex == '女')
			$sex = '2';

		if ($sex != '1' && $sex != '2')
			$sex = '0';

		/* 检查验证码是否正确 */
		$user = D('McUser');
		$user -> changeUserInfo($userID,$user_nicename,$sex,$email,$signature);

		$relation = D('McUsersRelationships');
		$relation -> save_nice_name($userID, $user_nicename);

		$this -> ajaxReturn($rejson);					
	}
}
